﻿using FluentScheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace BLL.JobService.JobList
{
    /// <summary>
    /// 默认任务
    /// </summary>
    public class Default : IJob, IRegisteredObject
    {
        private readonly object _lock = new object();
        private bool _shuttingDown;

        public Default()
        {
            HostingEnvironment.RegisterObject(this);
        }
        public void Execute()
        {
            try
            {
                lock (_lock)
                {
                    if (_shuttingDown) { return; }
                    //任务写在这里
                    //Evan.SystemLog.MemLog.AddExLog2("FluentScheduler 写入执行", null, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                    
                    //任务执行完成
                }
            }
            finally
            {
                HostingEnvironment.UnregisterObject(this);
            }

        }

        /// <summary>
        /// 任务结束后
        /// 设置 _shuttingDown 
        /// </summary>
        /// <param name="immediate"></param>
        public void Stop(bool immediate)
        {
            //调用stop
            lock (_lock)
            {
                //lock结束
                _shuttingDown = true;
            }
            HostingEnvironment.UnregisterObject(this);
        }
    }
}
